﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;


namespace hozoroghiab
{
    public class sql
    {
        string userid, password, server,database, timeout;
        SqlConnection myConnection;
        SqlCommand myCommand;
       // string connectionString = @"Data Source=HAMIDREZA-PC;Initial Catalog=absent;Integrated Security=True";
        public sql(string userid, string password, string server, string database, string timeout)
        {
             this.userid=userid;
             this.password=password;
             this.server = server;
             this.database=database;
             this.timeout=timeout;
        }
        public sql()
        {
            StreamReader sr = new StreamReader("database.ini");
            while (!sr.EndOfStream)
            {
                string msg = sr.ReadLine();
                string[] data = msg.Split('=');
                switch (data[0])
                {
                    case "userid":
                        this.userid = data[1];
                     break;
                    case "password":
                     this.password = data[1];
                     break;
                    case "server":
                     this.server = data[1];
                     break;
                    case "database":
                     this.database = data[1];
                     break;
                    case "timeout":
                     this.timeout = data[1];
                     break;
                    default:
                     throw new Exception("error input");
                     break;
                }
            }
        }
        public void connect()
        {
 
            try
            {
                myConnection = new SqlConnection(@"user id=" + userid + ";" +
                "password=" + password + ";server=" + server + ";" +
                "Trusted_Connection=yes;" +
                "database=" + database + "; " +
                "connection timeout=" + timeout + ";Integrated Security=True;");
            }
            catch
            {
                
                throw new Exception();

            }
        }
        public void open()
        {
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public void close()
        {
            try
            {
                myConnection.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
        public void ExecuteNonQuery(string sql)
        {
            myCommand = new SqlCommand(sql, myConnection);
            myCommand.ExecuteNonQuery();
        }
        public object ExecuteScalar(string sql)
        {
            myCommand = new SqlCommand(sql, myConnection);
            object a = (object)myCommand.ExecuteScalar();
            return a;
        }
        
        public SqlDataReader ExecuteQuery(string sql)
        {
            SqlDataReader myReader = null;
            try
            {
                myCommand = new SqlCommand(sql,myConnection);
                myReader = myCommand.ExecuteReader();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return myReader;
        }
        public SqlDataAdapter sqlcommand(string sql)
        {
            SqlDataAdapter da=null;
            
            try
            {
                da = new SqlDataAdapter(sql, myConnection);
                 SqlCommandBuilder sb = new SqlCommandBuilder(da); 
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
            return da;
           
        }
        
    }
}
